﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels/ExceptionDetailDialogViewModel
* 类       名: ExceptionDetailDialogViewModel
* 功      能:  TD
* 时      间:  2024-04-27 14:46:18
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace WaferSelectV3.ViewModels
{
    public class ExceptionDetailDialogViewModel : BindableBase, IDialogAware
    {
        public ExceptionDetailDialogViewModel()
        {
            CloseDialogCommand = new DelegateCommand(DoCloseDialog);
        }

        private void DoCloseDialog()
        {
            RequestClose?.Invoke(new DialogResult());
        }

        public DelegateCommand CloseDialogCommand { get; private set; }

        private string? title;

        public string? Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged();
            }
        }

        private Exception? exceptionSelected;

        public Exception? ExceptionSelected
        {
            get { return exceptionSelected; }
            set
            {
                exceptionSelected = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<Exception> exceptions = new ObservableCollection<Exception>();

        public ObservableCollection<Exception> Exceptions
        {
            get { return exceptions; }
            set
            {
                exceptions = value;
                RaisePropertyChanged();
            }
        }

        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Exceptions.Clear();
            parameters.TryGetValue("Title", out string title);
            parameters.TryGetValue("Exception", out Exception? exception);
            if (!string.IsNullOrEmpty(title))
            {
                Title = title;
            }
            while (exception != null)
            {
                Exceptions.Add(exception);
                // 当没有内部异常的时候exception.InnerException的返回值为null
                exception = exception.InnerException;
            }
            ExceptionSelected = Exceptions.FirstOrDefault();
        }
    }
}
